package org.imlinker.plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;

public class ResourceManager {

	private static ResourceManager resourceManager = null;

	private static String path;

	private static ChatManager chatManager;

	private static XMPPConnection connection;

	public static File getFile(String pluginName, String fileName) {
		String pluginDirPath = path + File.separator + "plugin"
				+ File.separator + pluginName;
		File pluginDir = new File(pluginDirPath);
		if (!pluginDir.exists()) {
			pluginDir.mkdir();
		}

		String filePath = pluginDir.getAbsolutePath() + File.separator
				+ fileName;
		return new File(filePath);
	}

	public static ResourceManager getInstance() {
		return resourceManager;
	}

	public static void setConnection(XMPPConnection newConnection) {
		connection = newConnection;

		chatManager = connection.getChatManager();
	}

	public static Chat getChat(String name) {
		return chatManager.createChat(name, null);
	}

	public static ArrayList<String> getBuddyNames() {
		Collection<RosterEntry> entris = connection.getRoster().getEntries();
		ArrayList<String> names = new ArrayList<String>();

		for (RosterEntry entry : entris) {
			String user = entry.getUser();
			if (user.indexOf("/") >= 0) {
				user = user.substring(0, user.indexOf("/"));
			}
			names.add(user);
		}

		return names;
	}

	public static void setPath(String newPath) {
		resourceManager = new ResourceManager();

		path = newPath;
	}

}
